<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layout 后台大布局 - Layui</title>
    <link rel="stylesheet" href="/ordermaker/pages/layui/css/layui.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">

    #include("common.html")

    <div class="layui-body">
        <div style="margin-left: 10px;margin-right: 10px;margin-top: 15px;">
            <table class="layui-hide" id="test" lay-filter="test"></table>
        </div>
    </div>

    <div class="layui-footer">
        <!-- 底部固定区域 -->
        © layui.com - 底部固定区域
    </div>
</div>
<script type="text/html" id="toolbarDemo">
    <form class="layui-form layui-form-pane" action="">

        <div class="layui-inline">
            <label class="layui-form-label">日期范围</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="date" lay-verify="required" autocomplete="off" id="test6" placeholder=" - ">
            </div>
        </div>

        <div class="layui-inline layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="exportData">导出数据</button>
        </div>
    </form>
</script>
<script type="text/html" id="barDemo">
    <!--<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>-->
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/ordermaker/pages/layui/layui.js"></script>
<script>
    //JavaScript代码区域
    layui.use(['element','table','laydate','form'], function(){
        var element = layui.element;
        var table = layui.table;
        var laydate = layui.laydate;

        table.render({
            elem: '#test'
            ,url:'/ordermaker/admin/luke/recordlist'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: [{ //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '录课记录表'
            ,cols: [[
                {field:'id', title:'ID', fixed: 'left'}
                ,{field:'name', title:'使用者'}
                ,{field:'day', title:'使用时间'}
                ,{field:'xingqi', title:'星期'}
                ,{field:'jieci', title:'节次'}
                ,{field:'sheying', title:'录课老师'}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,parseData: function(res){ //res 即为原始返回的数据
                console.log(res)
                return {
                    "code": res.erro, //解析接口状态
                    "count": res.datas.totalRow, //解析数据长度
                    "data": res.datas.list //解析数据列表
                };
            }
            ,page: true
        });

        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    layui.jquery.ajax({
                        url: "/ordermaker/admin/luke/delrecord",
                        type: 'post',//method请求方式，get或者post
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        },
                        xhrFields:{
                            withCredentials:true
                        },
                        dataType: 'json',//预期服务器返回的数据类型
                        data: {id:data.id},//表格数据序列化
                        success: function (res) {//res为相应体,function为回调函数
                            console.log(res);
                            layer.msg(data.name+"的录课已删除");
                            obj.del();
                            layer.close(index);
                        },
                        error: function (res) {
                            console.log(res);
                            layer.alert('操作失败！！！', {icon: 5});
                        }
                    });
                });
            } else if(obj.event === 'edit'){
                layer.prompt({
                    formType: 2
                    ,value: data.email
                }, function(value, index){
                    obj.update({
                        email: value
                    });
                    layer.close(index);
                });
            }
        });

        //日期范围
        laydate.render({
            elem: '#test6'
            ,range: true
        });

        var form = layui.form;

        //监听提交
        form.on('submit(exportData)', function(data){
            layui.jquery.ajax({
                url: "/ordermaker/admin/luke/exportdata",
                type: 'post',//method请求方式，get或者post
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                xhrFields:{
                    withCredentials:true
                },
                dataType: 'json',//预期服务器返回的数据类型
                data: data.field,//表格数据序列化
                success: function (res) {//res为相应体,function为回调函数
                    console.log(res);
                    location.href="/ordermaker"+res.file;
                    return;
                },
                error: function (res) {
                    console.log(res)
                    layer.alert('操作失败！！！', {icon: 5});
                }
            });

            return false;
        });
    });
</script>
</body>
</html>